//
//  MainWindow.m
//  MediaInfoGUI
//
//  Created by Olivier Larivain on 3/20/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "DropWindow.h"
#import "AppController.h"

@interface DropWindow(hidden)
- (BOOL) readFromPasteboard:(NSPasteboard *)pb;
@end

@implementation DropWindow
- (id) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
  [super initWithContentRect: contentRect styleMask:aStyle backing:bufferingType defer:flag];
  [self registerForDraggedTypes:[NSArray arrayWithObject: NSFilenamesPboardType]];
  return self;
}

-(NSDragOperation) draggingEntered:(id <NSDraggingInfo>)sender
{
  return NSDragOperationCopy;
}

-(BOOL) prepareForDragOperation:(id <NSDraggingInfo>)sender
{
  return YES;
}

-(BOOL) performDragOperation:(id <NSDraggingInfo>)sender
{
  NSPasteboard *pb = [sender draggingPasteboard];
  
  return [self readFromPasteboard:pb];
}

-(void) concludeDragOperation:(id <NSDraggingInfo>)sender
{
  if(droppedFiles)
  {
    [appController openFilenames: droppedFiles];          
  }
}

- (BOOL) readFromPasteboard:(NSPasteboard *)pb
{
  NSArray *types = [pb types];
  if([types containsObject: NSFilenamesPboardType])
  {
    NSArray *value = [pb propertyListForType: NSFilenamesPboardType];

    droppedFiles = value;
    return TRUE;
  }
  droppedFiles = nil;
  return FALSE;
}
@end
